package com.buyingtickets.controller;

import com.buyingtickets.dao.UserDao;
import com.buyingtickets.entity.User;
import com.buyingtickets.entity.UserSign;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

@Controller
@RequestMapping("login")
public class LoginAction {
    @Resource
    private UserDao userDao;

    @RequestMapping("Login")
    public String login(Model model , HttpSession httpSession){
        User u=null;
        try {
           u= (User) httpSession.getAttribute("User");
       }catch (Exception e){
           e.printStackTrace();
       }
        model.addAttribute("User",u);
        return "info";
    }

    @RequestMapping("tcdl")
    public String tcdl(HttpSession httpSession){
        httpSession.removeAttribute("User");
        return "info";
    }

    @RequestMapping("in")
    @ResponseBody
    public String toLogin( String name, String pwd, HttpSession httpSession) {
        User u = new User();
        u.setUserAccount(name);
        u.setUserPwd(pwd);
        System.out.println(u.getUserAccount()+" "+u.getUserPwd());
        User user = userDao.Login(u);
        System.out.println(user.getUserAccount());
        System.out.println(user.getUserPwd()+" "+ pwd);
        if (user == null) {
            return "0";
        } else if (!pwd.equals(user.getUserPwd())) {
            return "1";
        } else {
            if (user.getUserType().getTypeId() == 9) {
                httpSession.setAttribute("Admin", user);
                return "2";
            } else if (user.getUserType().getTypeId() == 8) {
                httpSession.setAttribute("User", user);
                return "3";
            }
        }
        return null;
    }
    @RequestMapping("tosign")
    public String toSign(Model model){
        if(model.getAttribute("zhucexinxi")==null || model.getAttribute("zhucexinxi")==""){
            model.addAttribute("zhucexinxi","null");
        }
        return "sign";
    }

    @RequestMapping("xiugai")
    public String xiugai(Model model,User user,HttpSession httpSession){

        userDao.updateUser(user);
        return toupdate(model,httpSession);
    }
    @RequestMapping("toupdate")
    public String toupdate(Model model,HttpSession httpSession){
        User user=(User)httpSession.getAttribute("User");
        User u=userDao.Login(user);
        model.addAttribute("u",u);
        return "usertaccount";
    }

    @RequestMapping("zhuce")
    public String zhuce(UserSign u, Model model){
        u.setUserType(9);
        User q=new User();
        System.out.println(u.getUserAccount());
        q.setUserAccount(u.getUserAccount());
        User user=userDao.Login(q);
        boolean falg=false;
        if(user==null) {
            falg = userDao.addUser(u);
            if(falg){
                return "info";
            }else{
                model.addAttribute("zhucexinxi","注册失败");
                return   toSign(model);
            }
        }else{
            model.addAttribute("zhucexinxi","该用户已存在");
            return  toSign(model);
        }

    }

    @RequestMapping("sjyz")
    @ResponseBody
    public int sjyz(String mobile) throws IOException {
        String appid="43413936";
        String appsecret="XzNOt5Rh";
        Random r=new Random();
        int code=r.nextInt(899999)+100000;
        InputStream in = null;
        InputStreamReader reader = null;
        URLConnection con = null;
        BufferedReader br = null;
        String str ="https://yiketianqi.com/api/sms?appid="+appid+"&appsecret="+appsecret+"&code="+code+"&mobile="+mobile;
        String result = "";
        try {
            URL url = new URL(str);
            con = url.openConnection();
            in = con.getInputStream();
            reader = new InputStreamReader(in, "utf-8");
            br = new BufferedReader(reader);
            String s1 = "";
            while ((s1 = br.readLine()) != null) {
                result += s1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null)
                reader.close();
            if (in != null)
                in.close();
        }
        System.out.println(result);
        String cg="";
        for(int i=23;i<30;i++) {
            cg+=result.charAt(i);
        }
        if(cg.equals("SUCCESS")) {
            return code;
        }else {
            return 0;
        }
    }
}
